import 'package:aio/aio.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:app/generated/app_strings.dart';
import 'package:app/src/http/bean/wroks/product_bean.dart';
import 'package:app/src/http/bean/wroks/special_clauses.dart';
import 'package:app/src/ui/route/works/public/items/ui_base.dart';
import 'package:app/src/ui/route/works/public/items/ui_card.dart';
import 'package:app/src/utils/image_path_utils.dart';

import '../../../../app_ui.dart';

import 'dart:convert' as convert;

///工艺信息
class UiProcessInfo {
  ///List<ProductStructureLayerInfo>? 工艺信息
  ///isOrder 是否为订单审核；（订单审核里有原片质量等级）
  static Widget build(
      BuildContext context, List<ProductStructureLayerInfo>? data, bool isOrder,
      {String? title}) {
    List<Widget> children = [];
    if (StringUtils.isEmpty(title)) {
      title = AppStrings.of(context).ProductStructureLayerInfo; //工艺信息
    }
    children.add(
        UiBase.title(context, title, bottom: AppEdgeInsets.margin_bottom)); //标题
    children.add(_table(context, data, isOrder));
    var column = UiBase.column(context, children);
    var card = UiCard.build(context, column);
    return card;
  }

  static Widget _table(
    BuildContext context,
    List<ProductStructureLayerInfo>? data,
    bool isOrder,
  ) {
    // var map = <String, dynamic>{};
    // map["ProductStructureLayerInfo"] = data;
    // var json=convert.jsonEncode(map);
    // print('数据：\t$json');
    // var json=listToJson(data);
    // print('数据：\t$json');
    List<TableRow> children = [];
    //标题
    List<Widget> titleChilder = [];
    titleChilder.add(Center(
      child: _text(AppStrings.of(context).GlassName), //材料名称
    ));
    titleChilder.add(Center(
      child: _text(AppStrings.of(context).GlassThin + "(mm)"), //厚度
    ));
    titleChilder.add(Center(
      child: _text(AppStrings.of(context).ProcessList), //工艺路线
    ));
    titleChilder.add(Center(
      child: _text(AppStrings.of(context).AdgeRequire), //磨边要求
    ));
    titleChilder.add(Center(
      child: _text(AppStrings.of(context).ManufacturerName), //品牌
    ));
    if (isOrder) {
      titleChilder.add(Center(
        child: _text(AppStrings.of(context).QualityGrade), //fixme 原片质量等级
      ));
    }
    titleChilder.add(Center(
      child: _text(AppStrings.of(context).BrandCoat), //镀膜牌号
    ));
    titleChilder.add(Center(
      child: _text(AppStrings.of(context).BrandCoatPosition), //膜面位置
    ));
    titleChilder.add(Center(
      child: _text(AppStrings.of(context).GlazeBrandCoatName), //彩釉牌号
    ));
    titleChilder.add(Center(
      child: _text(AppStrings.of(context).GlazeBrandCoatPosition), //彩釉位置
    ));
    titleChilder.add(Center(
      child: _text(AppStrings.of(context).BrandRequire), //商标
    ));
    titleChilder.add(Center(
      child: _text(AppStrings.of(context).BrandRequirePosition), //商标位置
    ));
    titleChilder.add(Center(
      child: _text(AppStrings.of(context).FrostPosition), //磨砂位置
    ));
    titleChilder.add(Center(
      child: _text(AppStrings.of(context).FilmPosition), //贴膜位置
    ));
    children.add(TableRow(
        //第一行，添加背景色
        decoration:
            BoxDecoration(color: AppColors.backgroundColorWithTableTitle),
        children: titleChilder));
    if (data != null && data.length > 0) {
      data.forEach((element) {
        //print('材料名称：\t${element.GlassName}');
        List<Widget> contentChilder = [];
        contentChilder.add(Center(
          child: _text(element.GlassName), //材料名称
        ));
        contentChilder.add(Center(
          child: _text(element.GlassThin), //厚度
        ));
        contentChilder.add(Center(
          child: _text(element.ProcessList), //工艺路线
        ));
        contentChilder.add(Center(
          child: _text(element.AdgeRequire), //磨边要求
        ));
        contentChilder.add(Center(
          child: _text(element.ManufacturerName), //品牌
        ));
        if (isOrder) {
          contentChilder.add(Center(
            child: _text(element.QualityGrade), //fixme 原片质量等级
          ));
        }
        contentChilder.add(Center(
          child: _text(element.BrandCoat), //镀膜牌号
        ));
        contentChilder.add(Center(
          child: _text(element.BrandCoatPosition), //膜面位置
        ));
        contentChilder.add(Center(
          child: _text(element.GlazeBrandCoatName), //彩釉牌号
        ));
        contentChilder.add(Center(
          child: _text(element.GlazeBrandCoatPosition), //彩釉位置
        ));
        contentChilder.add(Center(
          child: InkWell(
            child: _text(element.BrandRequire, isBlue: true),
            onTap: () {
              element.openBrandRequireImg(context); //查看商标图片
            },
          ), //商标
        ));
        contentChilder.add(Center(
          child: _text(element.BrandRequirePosition), //商标位置
        ));
        contentChilder.add(Center(
          child: _text(element.FrostPosition), //磨砂位置
        ));
        contentChilder.add(Center(
          child: _text(element.FilmPosition), //贴膜位置
        ));
        //内容
        children.add(TableRow(children: contentChilder));
      });
    }
    var table = Table(
      defaultVerticalAlignment: TableCellVerticalAlignment.middle,
      columnWidths: {
        0: FlexColumnWidth(2),
        2: FlexColumnWidth(3),
      },
      border: AppStyle.getTableBorder(),
      textDirection: TextDirection.ltr,
      children: children,
    );
    var width = 1800.0;
    if (width < px.screenWidth()) {
      width = px.screenWidth();
    }
    return SingleChildScrollView(
      scrollDirection: Axis.horizontal,
      physics: BouncingScrollPhysics(),
      child: Container(
        width: width,
        child: table,
      ),
    );
  }

  static Widget _text(String? text, {bool isBlue = false}) {
    TextStyle? style = null;
    if (isBlue) {
      style = AppStyle.textStyleWithCardBlue();
    } else {
      style = AppStyle.textStyleWithCard();
    }
    return Text(
      text ?? "",
      style: style,
    );
  }
}
